import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import UserList from '../views/UserList.vue'
import Abort from '../views/About.vue'
import Goods from '../views/Goods.vue'
import OrderList from '../views/OrderList.vue'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: {
      name: 'Login'
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    children: [{
        path: '/home',
        redirect: {
          name: 'Abort'
        }
      }, {
        path: '/getUserList',
        name: 'UserList',
        component: UserList
      },
      {
        path: '/about',
        name: 'Abort',
        component: Abort
      },
      {
        path: '/getGoodsList',
        name: 'Goods',
        component: Goods
      },
      {
        path: '/getOrdersList',
        name: 'OrderList',
        component: OrderList
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const token = sessionStorage.getItem("token")
  if (!token) return next('/login')
  next()

  // if(to.path === '/login'){
  //   next()
  // }else{
  //   const token = sessionStorage.getItem("token")
  //   if(!token){
  //     next('/login')
  //   }else{
  //     next()
  //   }
  // }
})

export default router
